﻿#region Using statements
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartFrequency.Utils;
using SmartFrequency.SoundCapture;
#endregion

namespace SmartFrequency.GUI
{
    public partial class Metronom : Form
    {
        #region Private variables
        private DateTime PTimer;
        private Utilities PUtils = new Utilities();
        private Language.Language_e PLanguage = Language.Language_e.TR;
        private Language ProgramLanguage;
        private SoundCapturePlay SoundObject;
        private Settings PSettings;
        private int Beat, BeatTime;

        enum OpState
        {
            STOPING = 0,
            PLAYING,
            RECORDING,
            PAUSING
        };

        private OpState OperationState;
        #endregion

        #region Private functions
        private void SetState(OpState value) { OperationState = value; }
        private OpState GetState() { return OperationState; }
        #endregion

        #region Public functions
        public Metronom()
        {
            InitializeComponent();
            this.Text = PUtils.Version;
            PSettings = new Settings("settings.txt");
            PSettings.Load();

            switch (PSettings.GetMember("language"))
            {
                default:
                case "TR":
                case "tr":
                    PLanguage = Language.Language_e.TR;
                    break;
                case "ENG":
                case "eng":
                    PLanguage = Language.Language_e.ENG;
                    break;
            }
            ProgramLanguage = new Language(PLanguage);

            SoundObject = new SoundCapturePlay();
            SoundObject.PlayInit();
            Beat = 60;


            //SoundObject.PlayFromMemory
        }

        private void IncreaseBeat_Click(object sender, EventArgs e)
        {
            Beat++;
            if (Beat > 360)
                Beat = 0;

            label3.Text = Convert.ToString(Beat);
        }

        private void DecreaseBeat_Click(object sender, EventArgs e)
        {
            Beat--;
            if (Beat < 0)
                Beat = 360;

            label3.Text = Convert.ToString(Beat);
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan tm = DateTime.Now - PTimer;
            if (tm.Milliseconds < BeatTime)
                return;

            SoundObject.PlayMemoryStart();
            PTimer = DateTime.Now;
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            SoundObject.Dispose();
            Dispose();
        }
        
        private void PlayMetronom_Click(object sender, EventArgs e)
        {
            switch (GetState())
            {
                case OpState.STOPING:
                    timer1.Interval = BeatTime = (60000 / Convert.ToInt16(label3.Text));
                    timer1.Enabled = true;
                    pictureBox3.Image = ımageList1.Images[1];
                    PTimer = DateTime.Now;
                    SetState(OpState.PLAYING);
                    break;

                case OpState.PLAYING:
                    timer1.Enabled = false;
                    pictureBox3.Image = ımageList1.Images[0];
                    SetState(OpState.STOPING);
                    break;

                default:
                    break;
            }
        }

        #endregion
    }
}